package actions;

import gui.MainFrame;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import tablemodels.CriterionModel;
import utils.Expert;


/**
 * 
 * @author dernasherbrezon
 *
 */
public class ExpertCriterionFiller extends WizardStep
{
	
	//data.size() == experts.size()
	
	private MainFrame parent;
	
	private ArrayList<CriterionModel> data = new ArrayList<CriterionModel>();
	
	public ExpertCriterionFiller(MainFrame parent)
	{
		this.parent = parent;
		BoxLayout bl = new BoxLayout(this,BoxLayout.Y_AXIS);
		setLayout(bl);
	}
	
	@Override
	public void checkInputValues() throws Exception
	{
		for(int i=0;i<data.size();i++)
		{
			data.get(i).checkCorrectValues();
		}
	}

	@Override
	public void makeActions()
	{
		int[] summCriterion = new int[parent.expertCriterions.size()];
		for(int i=0;i<parent.expertCriterions.size();i++)
		{
			for(int j=0;j<data.size();j++)
			{
				summCriterion[i] += (Integer)data.get(j).getValueAt(0,i);
			}
		}
		
		int rangSumm = 0;
		for(int i=0;i<summCriterion.length;i++)
		{
			rangSumm += summCriterion[i];
		}
		
		float[] vspomCalculation = new float[parent.expertCriterions.size()];
		
		for(int i=0;i<summCriterion.length;i++)
		{
			vspomCalculation[i] += (float)rangSumm/summCriterion[i];
		}
		
		float summVspomCalculation = 0.0f;
		
		for(int i=0;i<vspomCalculation.length;i++)
		{
			summVspomCalculation += vspomCalculation[i];
		}
		
		float testSumm = 0;
		for(int i=0;i<parent.expertCriterions.size();i++)
		{
			parent.expertCriterions.get(i).weight = (float)vspomCalculation[i]/summVspomCalculation;
			testSumm += parent.expertCriterions.get(i).weight;
		}
		
	}

	private void createTableForExpert(Expert expert)
	{
		add(new JLabel("Ранжирование критериев компетентности для эксперта: "+expert.name));
		CriterionModel tableModel = new CriterionModel(parent.expertCriterions);
		data.add(tableModel);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setReorderingAllowed(false);
		JScrollPane sp = new JScrollPane(table);

		JLabel corner = new JLabel("Критерии");
		corner.setBorder(table.getTableHeader().getBorder());
		sp.setCorner(JScrollPane.UPPER_LEFT_CORNER,corner);
		
		JTable rowHeader = new JTable(new DefaultTableModel()
		{
			@Override
			public int getRowCount()
			{
				return 1;
			}
			@Override
			public int getColumnCount()
			{
				return 1;
			}
			@Override
			public boolean isCellEditable(int row, int column)
			{
				return false;
			}
			@Override
			public Object getValueAt(int row, int column)
			{
				return new String("Ранги");
			}
		});
		rowHeader.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer()
		{
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
			{
				JLabel label = new JLabel((String)value);
				return label;
			}
		});
		JViewport viewport = new JViewport();
        viewport.setView(rowHeader);
		//TODO
		// if error----> table.setBorder(new LineBorder(Color.RED));

        Dimension bestPrefSize = new Dimension();
        bestPrefSize.height = Math.max(rowHeader.getPreferredSize().height,table.getPreferredSize().height);
        bestPrefSize.width = Math.max(rowHeader.getPreferredSize().width, corner.getPreferredSize().width);
        viewport.setPreferredSize(bestPrefSize);
        sp.setRowHeader(viewport);
		add(sp);
	}

	@Override
	public void loadStep()
	{
		removeAll();
		for(int i=0;i<parent.experts.size();i++)
		{
			createTableForExpert(parent.experts.get(i));
		}
	}
	
	@Override
	public void preShow()
	{
	}
}
